//
//  GYUserAccountViewModel.swift
//  Weibo
//
//  Created by vane on 16/8/11.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class GYUserAccountViewModel: NSObject {
    
    static let sharedTools:GYUserAccountViewModel = GYUserAccountViewModel()
    
    var userAccount:GYAccessTokenModel?
    
    override init(){
        
        super.init()
        userAccount = getUserAccount()
    }
    
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    var accessToken: String?{
        if userAccount?.access_token != nil {
            // 如果微博返回来的的时间 和当前的时间比较为降序 就代表没有过期
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else{
                return nil
            }
            
        }else {
            // 代表用户没有登录过
            return nil
        }
    }
    
    // 判断用户是否登录(如果accessToken为nil 就代表没有登录)
    var isLogin: Bool {
        return accessToken != nil
    }

}
//保存数据
extension GYUserAccountViewModel{
    
    func saveUserAccount(userAccount: GYAccessTokenModel){
//        // 路径
//        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
        self.userAccount = userAccount
        // 保存对象
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    }
    
    // 获得对象
    func getUserAccount() -> GYAccessTokenModel?{
        // 路径
//        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
        // 获取对象
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? GYAccessTokenModel
        return result
    }

    
}

//请求数据
extension GYUserAccountViewModel{
    // 获取授权
    func getAccessToken(code:String, finish:(isSuccess: Bool)->()){
        
        AFNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
            guard let res = response as? [String:AnyObject] else{
                
                finish(isSuccess: false)

                return
            }
            
            let accessToken = GYAccessTokenModel(dict: res)
            
//            print(accessToken)
            self.getUserInfo(accessToken, finish: finish)
            
            }) { (error) -> () in
                print("error:\(error)")
                finish(isSuccess: false)

        }
    }
    // 请求个人信息
    func getUserInfo(accessToken:GYAccessTokenModel, finish:(isSuccess: Bool)->()){
        
        AFNetworkTools.sharedTools.oauthLoadUserInfo(accessToken, success: { (response) -> () in
            
            guard let res = response as? [String:AnyObject] else{
                
                finish(isSuccess: false)

                return
            }
            accessToken.screen_name = res["screen_name"] as? String
            accessToken.avatar_large = res["avatar_large"] as? String
            
            print(accessToken)
//            print("success:\(response)")
            // 保存对象
            self.saveUserAccount(accessToken)

            finish(isSuccess: true)
            
            }) { (error) -> () in
                print("error:\(error)")
                finish(isSuccess: false)

                
        }
    }

}
